package com.topone.service.impl;

import com.lly835.bestpay.config.WxPayH5Config;
import com.topone.Exception.FanZoneServiceException;
import com.topone.Utils.KeyUntil;
import com.topone.Utils.MathUtil;
import com.topone.dataObject.Bussiness;
import com.topone.dataObject.OrderMaster;
import com.topone.dataObject.PayNotification;
import com.topone.service.BaseService;
import com.topone.service.NotificationService;
import com.topone.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.util.RandomUtils;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.pay.request.WxPayUnifiedOrderRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
@Slf4j
public class BaseServiceImpl implements BaseService {

    @Autowired
    private NotificationService notificationService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private WxPayH5Config wxPayH5Config;
    @Autowired
    private WxMpService wxService;

    @Override
    public Map<String, Object> getPay(String amount, String openid, double total, Bussiness bussiness,String type,String couponCode) {
        Map<String, Object> tempMap = new HashMap<>();
        //下单
        OrderMaster orderMaster=null;
        if(StringUtils.isNotEmpty(amount)) {
             orderMaster = orderService.checkUserCoupon(amount, openid);
        }
        //2.发起支付
        WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
        request.setAppid(wxPayH5Config.getAppId());
        request.setMchId(wxPayH5Config.getMchId());
        request.setOpenid(openid);
        request.setTradeType("JSAPI");
        request.setNotifyURL(wxPayH5Config.getNotifyUrl());
        request.setDeviceInfo("WEB");
        request.setBody("fanZone－用户买单支付");
        request.setOutTradeNo(RandomUtils.getRandomStr());
        request.setTotalFee(MathUtil.Yuan2Fen((total)));
        request.setSpbillCreateIp("8.8.8.8");
        request.setNonceStr(RandomUtils.getRandomStr());
        Map<String, String> resultMap = new HashMap<>();
        try {
            resultMap = wxService.getPayService().getPayInfo(request);
            PayNotification data = new PayNotification();
            data.setOutTradeNo(request.getOutTradeNo());
            data.setPrepayId(resultMap.get("package"));
            if(Objects.nonNull(orderMaster)) {
                data.setOrderId(orderMaster.getOrderId());
            }else{
                data.setOrderId(KeyUntil.getUniqueKey());
            }
            data.setType(type);
            data.setBussinessId(bussiness.getBussinessId());
            data.setBussinessName(bussiness.getBussinessName());
            data.setOpenid(openid);
            data.setCouponCode(couponCode);
            data.setFlag("YES");
            log.info("----------data--------{}",data);
            notificationService.save(data);
            log.debug("生成预付单成功:{}", resultMap);
        } catch (Exception e) {
            log.error("{}", e.fillInStackTrace());
            new FanZoneServiceException("统一下单处理错误");
        }
        tempMap.put("timeStamp", resultMap.get("timeStamp"));
        tempMap.put("nonceStr", resultMap.get("nonceStr"));
        tempMap.put("packAge", resultMap.get("package"));
        tempMap.put("signType", resultMap.get("signType"));
        tempMap.put("paySign", resultMap.get("paySign"));
        tempMap.put("appId", resultMap.get("appId"));

        return tempMap;
    }
}
